<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formItem.name" :disabled="formItem.isExist" placeholder="请输入名称...." />
      </el-form-item>
      <!-- <el-form-item label="编码" prop="code">
        <el-input v-model="formItem.code" placeholder="请输入编码...." />
      </el-form-item> -->
      <el-form-item label="工单类型" prop="code">
        <el-select v-model="formItem.code" placeholder="请选择工单类型" clearable @change="typeChange">
          <el-option v-for="(item) in workTypeList" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定表单" prop="formIds">
        <el-select v-model="formItem.formIds" multiple placeholder="请选择表单">
          <el-option v-for="(item) in formList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定角色" prop="roles">
        <el-select v-model="formItem.roles" multiple placeholder="请选择角色">
          <el-option v-for="(item) in availableRoles" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/businessProcess'
import { list } from '@neu/dcam-ui/api/workflowForm'
import { list as roleList } from '@neu/auth-ui/api/role'
import { list as dList } from '@neu/dcam-ui/api/dictionary'
// 定义一个表单原始项
const formItem = {
  name: '',
  code: '',
  formIds: [],
  roles: [],
  isExist: false
}
export default {
  name: 'ProcessEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      /** 表单列表 */
      formList: [],
      // 工单类型
      workTypeList: [],
      /** 角色列表 */
      availableRoles: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '工单类型不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (/^[a-zA-Z]+[a-zA-Z0-9_]+$/.test(value) === false) {
              callback(new Error('工单类型编码只能含有字符、数字和下划线但是不能以数字开头'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
        ]
      },
      modal: true
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
    roleList().then(response => {
      this.availableRoles = response.data
    })
    dList({ s_type: 'workorderType' }).then(res => { this.workTypeList = res.data })
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          const data = response.data
          if (data.code) {
            this.getFormList(data.code)
          }
          this.formItem = {
            ...data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    /** 获取表单 */
    getFormList(type) {
      list({ s_workType: type }).then(res => {
        this.formList = res.data
      })
    },
    /** 工单类型发生变化 */
    typeChange(val) {
      this.formList = []
      this.formItem.formIds = []
      if (val) {
        this.getFormList(val)
      }
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
